package itsm.isperp.module.controller.configuration;

import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.configuration.ConfCi;
import itsm.isperp.module.entity.configuration.ConfCiRelationPlan;
import itsm.isperp.module.entity.configuration.ConfCiRelationType;
import itsm.isperp.module.service.configuration.ConfCiRelationPlanService;
import itsm.isperp.module.service.configuration.ConfCiRelationTypeService;
import itsm.isperp.module.service.configuration.ConfCiService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 配置关系控制器
 * 
 * @author lizx
 * @date 2014-09-22 09:52
 */
@Controller
@RequestMapping("configuration/ciRelationPlan")
public class ConfCiRelationPlanController extends BaseController<ConfCiRelationPlan> {

	@Autowired
	protected ConfCiRelationPlanService ConfCiRelationPlanService;
	@Autowired
	protected ConfCiRelationTypeService confCiRelationTypeService;
	@Autowired
	protected ConfCiService confCiService;

	@Override
	public ConfCiRelationPlanService getService() {
		return this.ConfCiRelationPlanService;
	}

	@Override
	public String getPageTitle() {
		return "配置关系";
	}

	@RequestMapping("listDetail")
	@ResponseBody
	@Override
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {
		String fromId = req.getParameter("fromId");
		request.setSearchString("from_id='" + fromId + "' OR to_id='" + fromId
				+ "'");
		return this.getService().findAllDetailByRequest(request).toPage();

	}

	/**
	 * 批量保持关联
	 * 
	 * @param changeId
	 * @param relationTypeId
	 * @param fromId
	 * @param toIds
	 * @param req
	 * @return
	 */
	@RequestMapping("batchSave")
	@ResponseBody
	public ResultMessage batchSave(String changeId, String relationTypeId,
			String fromId, String toIds, HttpServletRequest req) {

		String[] toIdArr = toIds.split(",");

		this.ConfCiRelationPlanService.batchSave(changeId, relationTypeId, fromId,
				toIdArr);
		return ResultMessage.success();
	}
	
	@RequestMapping("saveCiRelation")
	@ResponseBody
	public ResultMessage saveCiRelation(String fromId,
			String relationType, String toCiIds, HttpServletRequest req) {
		String[] toIds=toCiIds.split(",");
		String changeId=req.getParameter("changeId");
		String ciId=req.getParameter("ciId");
		for (String s : toIds) {
			ConfCiRelationPlan entity = new ConfCiRelationPlan();
			entity.setFromId(fromId);
			entity.setRelationTypeId(relationType);
			entity.setToId(s);
			entity.setChangeId(changeId);
			entity.setCiId(ciId);
			entity.setOpType("ciAdd");
			this.getService().saveOrUpdate(entity); 
		}
		return ResultMessage.success();
	}
	@RequestMapping("saveCiRelationIsToCi")
	@ResponseBody
	public ResultMessage saveCiRelationIsToCi(String fromId,
			String relationType, String toCiIds, HttpServletRequest req) {
		String[] fromIds=fromId.split(",");
		String changeId=req.getParameter("changeId");
		String ciId=req.getParameter("ciId");
		for (String s : fromIds) {
			ConfCiRelationPlan entity = new ConfCiRelationPlan();
			entity.setToId(toCiIds);
			entity.setRelationTypeId(relationType);
			entity.setFromId(s);
			entity.setChangeId(changeId);
			entity.setCiId(ciId);
			entity.setOpType("ciAdd");
			this.getService().saveOrUpdate(entity); 
		}
		return ResultMessage.success();
	}
	
	@RequestMapping("listCiRelation")
	@ResponseBody
	public List<Map<String,Object>> listCiRelation(String ciId,String changeId, HttpServletRequest req) {
		// 根据ciId 获取有资产数据的关系
		return this.getService().listCiRelation(ciId,changeId);
	}
	
	@RequestMapping("listRelateCi")
	@ResponseBody
	public DataResponse<Map<String,Object>> listRelateCi(String ciId,String categoryId,String type,String changeId, HttpServletRequest req,JqGridRequest request) {
		// 根据ciId 获取有资产数据的关系
		String filter="";
		filter=" (treenode_path like '%"+categoryId+"%' and relation_ci_id='"+ciId+"')";
		request.setSearchString(filter);
		request.addFilterRule("change_id", JqGridFilterRuleOp.eq, changeId);
		if(type.equals("from_ci"))
		{
			return this.getService().findRelateToCiByRequest(request);	
		}else if(type.equals("to_ci"))
		{
			return this.getService().findRelateFromCiByRequest(request);
		}else
		{
			return null;
		}
	}
	
	@RequestMapping("listCiRelationType")
	@ResponseBody
	public List<Map<String,Object>> listCiRelationType(String categoryId, HttpServletRequest req) {
		// 根据ciId 获取有资产数据的关系
		return this.getService().listCiRelationType(categoryId);
	}
	
	@RequestMapping("listCiRelationTypeByCiId")
	@ResponseBody
	public List<Map<String,Object>> listCiRelationTypeByCiId(String ciId, HttpServletRequest req) {
		// 根据ciId 获取有资产数据的关系
		return this.getService().listCiRelationTypeByCiId(ciId);
	}
	
	
	
	/**
	 * 列出关联关系的ci
	 * 
	 * @param fromId
	 * @param relationTypeId
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping("listRelation")
	@ResponseBody
	public DataResponse<ConfCi> listRelation(String fromId,
			String relationTypeId, JqGridRequest request, HttpServletRequest req) {
		// 根据fromId获取它的分类
		// 获取可以的关联列表
		ConfCiRelationType rt = this.confCiRelationTypeService
				.findOne(relationTypeId);

		request.addFilterRule("conf_category_id", JqGridFilterRuleOp.eq,
				rt.getToCategoryId());

		request.addFilterRule("id", JqGridFilterRuleOp.ne, fromId);
		request.setSearchString("  id NOT IN (SELECT to_id FROM conf_ci_relation_plan WHERE from_id='"
				+ fromId + "')");

		return this.confCiService.findAll(request);
	}

	/**
	 * 重建所有关联关系
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("rebuildAll")
	@ResponseBody
	public ResultMessage rebuildAll(HttpServletRequest req) throws Exception {

		String id = req.getParameter("id");
		String relationTypeId = req.getParameter("relationTypeId");

		if (StringUtils.isNotEmpty(id)) {
			String changeId = req.getParameter("changeId");
			this.ConfCiRelationPlanService.rebuildCiRelationByCi(id,changeId);
		} else if (StringUtils.isNotEmpty(relationTypeId)) {
			this.ConfCiRelationPlanService
					.rebuildCiRelationByRelationType(relationTypeId);
		} else {
			this.ConfCiRelationPlanService.rebuildCiRelation();
		}

		return ResultMessage.success();
	}
	
	@RequestMapping("listCi")
	@ResponseBody
	public PageResponse<Map<String, Object>> listCi(JqGridRequest request,
			HttpServletRequest req) {
		String categoryId=req.getParameter("categoryId");
		if(StringUtils.isNotEmpty(categoryId))
		{
			request.setSearchString(" b.treenode_path like '%"+categoryId+"%'");
		}
		return this.confCiService.findAllDetailByRequest(request).toPage();
	}
	
	@RequestMapping("listCiEx")
	@ResponseBody
	public PageResponse<Map<String, Object>> listCiEx(JqGridRequest request,
			HttpServletRequest req) {
		String categoryId=req.getParameter("categoryId");
		String relationTypeId=req.getParameter("relationTypeId");
		String ciId=req.getParameter("ciId");
		String ty=req.getParameter("ty");
		if(StringUtils.isNotEmpty(categoryId))
		{
			if(ty.equals("from_ci"))
			{
				request.setSearchString(" (b.treenode_path like '%"+categoryId+"%') and  a.id not in (select to_id from conf_ci_relation_plan where relation_type_id='"+relationTypeId+"' and from_id='"+ciId+"')");
				
			}else
			{
				request.setSearchString(" (b.treenode_path like '%"+categoryId+"%') and  a.id not in (select from_id from conf_ci_relation_plan where relation_type_id='"+relationTypeId+"' and to_id='"+ciId+"')");
			}
		}
		return this.confCiService.findAllDetailByRequest(request).toPage();
	}
	
	@RequestMapping("getCiRelateCount")
	@ResponseBody
	public String getCiRelateCount(String ciId,
			HttpServletRequest req) {
		
		return "";
	}
	
	@RequestMapping("getPlanChgCiRelateCount")
	@ResponseBody
	public Integer getPlanChgCiRelateCount(String ciId,String changeId,
			HttpServletRequest req) {
		return this.getService().getPlanChgCiRelateCount(ciId, changeId);
	}
	
	@RequestMapping("copyRelationByCiId")
	@ResponseBody
	public ResultMessage copyRelationByCiId(String ciId,String changeId,
			HttpServletRequest req) {
		this.getService().copyRelationByCiId(ciId,changeId);
		return ResultMessage.success();
	}
	
	@RequestMapping("updateRelateStatus")
	@ResponseBody
	public ResultMessage updateRelateStatus(String id,String opType, HttpServletRequest req) {
		this.getService().updateRelateStatus(id,opType);
		return ResultMessage.newInstance(true);
	}

}
